package jy.game.tools;

import com.randy.fundation.JxFunc;

import java.util.HashMap;
import java.util.Map;

public class JyResourceManager {
    private static Map<String, Map<String, Object>> values = new HashMap<>();

    public static <T> T get(Class<T> type, String key) {
        String              typeName   = type.getSimpleName();
        Map<String, Object> typeValues = JyResourceManager.values.get(typeName);
        if (typeValues == null) {
            return null;
        }

        if (typeValues.containsKey(key)) {
            return JxFunc.<Object, T>as(typeValues.get(key)).value();
        }
        return null;
    }

    public static <T> Iterable<T> getAll(Class<T> type) {
        String              typeName   = type.getSimpleName();
        Map<String, Object> typeValues = JyResourceManager.values.get(typeName);
        if (typeValues == null) {
            return null;
        }
        return (Iterable<T>) typeValues.values();
    }

    public static <T> void add(Class<T> type, String pk, T obj) {
        String              typeName   = type.getSimpleName();
        Map<String, Object> typeValues = JyResourceManager.values.get(typeName);
        if (typeValues == null) {
            typeValues = new HashMap<>(1);
            JyResourceManager.values.put(typeName, typeValues);
        }
        typeValues.put(pk, obj);
    }
}
